package annotation;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @author zhanglinfeng
 * @date create in 2024/12/12 15:23
 */
@Aspect
public class RepeatSubmitAspect {

    @Resource
    private RedissonClient redissonClient;

    @Pointcut("@annotation(annotation.RepeatSubmit)")
    public void pointcut() {
    }

    @Before("pointcut()&&@annotation(repeatSubmit)")
    public void doAspect(RepeatSubmit repeatSubmit) throws Exception {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String url = request.getRequestURI();
//        String userId = String.valueOf(request.getAttribute("user_id"));
//        if (url.startsWith("/")) {
//            url = url.substring(1);
//        }
//        if (!url.endsWith("/")) {
//            url = url + "/";
//        }
//        //防重复点击
//        String lockKey = url.concat(request.getMethod()).concat("/").concat(userId);
//        RLock rLock = redissonClient.getLock(lockKey);
//        if (rLock.isLocked()) {
//            throw new Exception(repeatSubmit.message());
//        } else {
//            rLock.lock(repeatSubmit.millisecond(), TimeUnit.MILLISECONDS);
//        }
    }
}
